package com.bqy.myokgo.base.permissions;

import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

import com.bqy.myokgo.base.MyBaseActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 熊猫先生
 * 2018/10/24:10:43
 * 权限申请基类
 */
public abstract class MyPermissionsActivity extends MyBaseActivity {
    //权限请求标识
    public static final int BASE_PERMISSIONS_ACTIVITY_REQUEST_CODE = 500;

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode != BASE_PERMISSIONS_ACTIVITY_REQUEST_CODE)
            return;
        ArrayList<String> Permit = new ArrayList<>();
        ArrayList<String> notPermit = new ArrayList<>();
        ArrayList<Boolean> notPermitBool = new ArrayList<>();
        for (int i = 0; i < grantResults.length; i++) {
            if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {//授权
                Permit.add(permissions[i]);
            } else {//未授权
                notPermit.add(permissions[i]);
                notPermitBool.add(ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[i]));
            }
        }
        //权限请求结果
        requestPermissionsResult(Permit, notPermit, notPermitBool);
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    /**
     * 请求权限
     */
    protected void requestPermission(String[] permissions) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && getNotPermitPermissions(permissions).length > 0) {
            requestPermissions(permissions, BASE_PERMISSIONS_ACTIVITY_REQUEST_CODE);
        } else {
            requestPermissionsResult(new ArrayList<String>(), new ArrayList<String>(), new ArrayList<Boolean>());
        }
    }

    /**
     * 权限请求结果
     *
     * @param Permit        得到许可的权限
     * @param notPermit     未得到许可的权限
     * @param notPermitBool 下标对应notPermit,是否拒绝再次显示
     *                      子类重写
     */
    protected abstract void requestPermissionsResult(ArrayList<String> Permit, ArrayList<String> notPermit, ArrayList<Boolean> notPermitBool);

    /**
     * 得到未许可的权限
     *
     * @param permissions 需要请求的权限数组
     */
    protected String[] getNotPermitPermissions(String[] permissions) {
        List<String> data = new ArrayList<>();
        for (int i = 0; i < permissions.length; i++) {
            if (!checkPermission(permissions[i])) {
                data.add(permissions[i]);
            }
        }
        if (data.size() < 0) {
            return new String[0];
        } else {
            return data.toArray(new String[data.size()]);
        }
    }

    /**
     * 检查权限是否得到许可
     *
     * @param permission 需要检查的权限
     * @return true  得到许可
     */
    protected boolean checkPermission(String permission) {
        if (ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED) {
            return true;
        }
        return false;
    }
}

